package com.sun.fun.controller;

import com.sun.fun.bean.Furn;
import com.sun.fun.service.FurnService;
import com.sun.fun.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@RestController // 由于是前后端分离，所以返回的都是json数据
@RequestMapping("/furn")
public class FurnController {
    // 注入service的bean对象，可以调用IService的方法
    @Resource
    private FurnService furnService;

    @PostMapping("/save")
    /*
     * 注意：这里的requestBody表示前端必须以json格式发送数据
     * 但是，如果使用表单提交的数据则不能添加这个注解 */
    public Result save(@RequestBody Furn furn) {
        log.info("furn = {} " + furn);
        furnService.save(furn);
        return Result.success();
    }

    /**
     * 获取数据列表
     *
     * @return /
     */
    @GetMapping("/listFurn")
    public Result<List<Furn>> listFurn() {
        log.info("listFurn家具列表数据");
        List<Furn> furnList = furnService.list();
        System.out.println(furnList);
        return Result.success(furnList);
    }

    @DeleteMapping("/del")
    public Result del(@RequestParam Integer id) {
        log.info("furn = {} " + id);
        furnService.removeById(id);
        return Result.success();
    }
    @PutMapping("/update")
    public Result update(@RequestBody Furn furn) {
        log.info("furn = {} " + furn);
        furnService.updateById(furn);
        return Result.success("修改成功");
    }
}

